package cn.edu.swufe.surverpark.struts2.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.edu.swufe.surverpark.model.Right;
import cn.edu.swufe.surverpark.model.Role;
import cn.edu.swufe.surverpark.struts2.dao.BaseDao;
import cn.edu.swufe.surverpark.struts2.service.RightService;
import cn.edu.swufe.surverpark.struts2.service.RoleService;
import cn.edu.swufe.surverpark.util.DataUtil;
import cn.edu.swufe.surverpark.util.StringUtil;
import cn.edu.swufe.surverpark.util.ValidateUtil;

/**
 *@author 陈龙 E-mail:529914446@qq.com
 *@version创建时间:2015年5月6日上午2:14:19
 *类说明
 */
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role> implements
		RoleService {

	@Resource(name="roleDao")
	public void setDao(BaseDao<Role> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}
	
	@Resource(name="rightService")
	private RightService rightService;

	@Override
	public void saveOrUpdateRole(Role model, Integer[] ids) {
		if(!ValidateUtil.isValid(ids)){
			model.getRights().clear();
		}else{
			List<Right> rights = rightService.findRightsInRange(ids);
			model.setRights(new HashSet<Right>(rights));
		}
		this.saveOrUpdateEntity(model);
	}

	@Override
	public List<Role> findRolesNotInRange(Set<Role> roles) {
		if(!ValidateUtil.isValid(roles)){
			return this.findAllEntities();
		}else{
			String hql = "From Role r WHERE r.id not in (" + DataUtil.extractRightsIds(roles) + ")";
			return this.findEntityByHQL(hql);
		}
	}
	

	@Override
	public List<Role> findRolesInRange(Integer[] ids) {
		if(ValidateUtil.isValid(ids)){
			String hql = "From Role r WHERE r.id in (" + StringUtil.arr2Str(ids) + ")";
			return this.findEntityByHQL(hql);
		}
		return null;
	}


}

